package com.lzx.iteam.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.AsyncTask;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.*;
import com.lzx.iteam.R;
import com.lzx.iteam.bean.GroupManageCreate;
import com.lzx.iteam.task.DownloadImageTask;
import com.lzx.iteam.util.ImageUtil;
import com.lzx.iteam.util.StringUtil;

import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;

/**
 * Created by shijunting on 15-1-22.
 */
public class GroupGridAdapter extends BaseAdapter {

    private Context mContext;
    private ArrayList<GroupManageCreate> list;
    public int type;

    public GroupGridAdapter(Context context) {
        this.mContext = context;
        list = new ArrayList<GroupManageCreate>();
//        GroupMemberData groupMemberData = new GroupMemberData();
//        groupMemberData.contact_name = "添加";
//        list.add(groupMemberData);
    }


    /**
     * 设置号码簿的管理员Grid的样式
     *
     * @param type type=1表示创建者点击管理员(若已经有管理员，显示添加，删除。无管理员显示添加)
     *             type=2表示点击删除后，显示删除标识，GridView最后一个item显示完成
     *             type=3表示普通用户点击管理员，不显示添加删除，只显示管理员列表
     */
    public void bindData(int type, ArrayList<GroupManageCreate> list) {
        this.list.clear();
        this.list = list;
        this.type = type;
        switch (type) {
            case 1:
                int size = 0;
                if (list.size() > 0) {
                    size = 2;
                } else {
                    size = 1;
                }
                for (int i = 0; i < size; i++) {
                    GroupManageCreate groupMemberData = new GroupManageCreate();
                    if (i == 0) {
                        groupMemberData.setContactName("添加");
                    } else {
                        groupMemberData.setContactName("删除");
                    }
                    list.add(groupMemberData);
                }
                notifyDataSetChanged();
                break;
            case 2:
                list.remove(list.get(list.size()-1));
                list.remove(list.get(list.size()-2));
                GroupManageCreate groupMemberData = new GroupManageCreate();
                groupMemberData.setContactName("完成");
                list.add(groupMemberData);
                notifyDataSetChanged();
                break;
            case 3:
                if (list.size() > 0) {
                    notifyDataSetChanged();
                }
                break;

        }
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = LayoutInflater.from(mContext).inflate(R.layout.group_grid_item, null);
            holder.img = (ImageView) convertView.findViewById(R.id.iv_grid_manager_img);
            holder.name = (TextView) convertView.findViewById(R.id.tv_grid_manager_name);
            holder.delete = (ImageView) convertView.findViewById(R.id.iv_grid_delete_icon);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        holder.name.setText(list.get(position).getContactName());
            String imageUrl = list.get(position).getContactImage();
            if (!StringUtil.isEmpty(imageUrl)) {
                holder.img.setTag(imageUrl);
                DownloadImageTask task = new DownloadImageTask(holder.img, imageUrl);
                task.execute(imageUrl);
            }
        return convertView;
    }

    private class ViewHolder {
        ImageView img;
        ImageView delete;
        TextView name;
    }


}
